home *** CD-ROM | disk | FTP | other *** search
- /************************************************************
- *
- *
- * Unit to look after source code.
- *
- * by Adrian Bool in cooperation with Graham Cox.
- *
- * copyright © phantasm coding 1992.
- *
- *
- ************************************************************/
-
- #include "Global.h"
- #include "Source.h"
-
- #include "Source.proto.h"
-
- /* public */
-
- sHandle newSource(void)
- {
- sHandle temp;
-
- temp = (sHandle) NewHandle(sizeof(source));
-
- if (temp != nil)
- {
- (*temp)->storage = nil;
- (*temp)->caret = 0;
- }
- return(temp);
- }
-
- void disposeSource(sHandle theSource)
- {
- if (theSource != nil)
- {
- if ((*theSource)->storage != nil)
- DisposHandle((Handle) (*theSource)->storage);
-
- DisposHandle((Handle) theSource);
- }
- }
-
- void loadSource(sHandle sourceCode)
- {
- short fileHandle;
- long size,x;
- sStHandle storage;
- SFReply fSpec;
- OSErr theErr;
- int fPath;
-
- if (sourceCode != nil)
- {
- fSpec = (*sourceCode)->fileSpec;
-
- if (! FSOpen(&fSpec.fName,fSpec.vRefNum,&fPath))
- {
- if (! GetEOF(fPath,&size))
- {
- size++; /* make room for terminating 0 */
-
- storage = (sStHandle) NewHandle(size);
- if (storage != nil)
- {
- HLock((Handle) storage);
- theErr = FSRead(fPath,&size,*storage);
- HUnlock((Handle) storage);
- if (!theErr)
- AsmError = FileOperationFailed;
- }
- else
- AsmError = NotEnoughMemory;
- }
- else
- AsmError = FileOperationFailed;
-
- theErr = FSClose(fPath);
- }
- else
- AsmError = FileOperationFailed;
-
- (*storage)[size] = '\0';
-
- for(x=0 ; x <= size ; x++) (*storage)[x] = tolower((*storage)[x]);
-
- (*sourceCode)->size = size;
- (*sourceCode)->storage = storage;
- }
- }
-
-
- void getSection(pHandle theProgram , char *string)
- {
- int x = 0;
- char ch;
- char **startHandle;
- long caretPos;
- sHandle sourceCode;
-
- sourceCode = (*theProgram)->sourceCode;
- startHandle = (*sourceCode)->storage;
- caretPos = (*sourceCode)->caret;
-
- while(isspace(ch = (*startHandle)[caretPos]) && (caretPos <= (*sourceCode)->size))
- {
- if (ch == '\r') (*sourceCode)->line++;
- caretPos++;
- }
-
- if ((*startHandle)[caretPos] == '\0')
- AsmError = UnexpectedEndOfFile;
- else
- {
- ch = (*startHandle)[caretPos];
- while(isgraph(ch) && (ch != '\0') && (x <= 255))
- {
- string[x] = ch;
- x++; caretPos++;
- ch = (*startHandle)[caretPos];
- }
-
- if (x > 255)
- AsmError = SectionTooLong;
- else
- string[x] = 0;
- }
- (*sourceCode)->caret = caretPos;
- }
-
-
- short getSegment(char *theSection , short *position , str255 theResult)
- {
- short x = 0;
-
- while(theSection[*position] == ',') (*position)++; /* get to next data item */
-
- while((theSection[*position] != ',') && (theSection[*position] != '\0')) /* read in item */
- {
- theResult[x] = theSection[*position];
- (*position)++; x++;
- }
-
- theResult[x] = '\0'; /* terminate string */
-
- if (theResult[0] == '\0') return(false);
- else return(true);
- }
-
- operator getPart(char *theSegment , short *position , str255 theResult)
- {
- short x = 0;
- operator theOperator;
- char ch;
-
- if (theSegment[*position] == '\0')
- {
- theResult[0] = '\0';
- return(End);
- }
-
- ch = theSegment[*position];
-
- if (isalnum(ch) || (ch == '%') || (ch == '$'))
- theOperator = None;
- else
- {
- switch (ch)
- {
- case '+' : theOperator = Add; break;
- case '-' : theOperator = Subtract; break;
- case '~' : theOperator = RevSubtract; break;
- case '*' : theOperator = Multiply; break;
- case '/' : theOperator = Divide; break;
- case '\\' : theOperator = RevDivide; break;
- case '&' : theOperator = And; break;
- case '|' : theOperator = Or; break;
- case '!' : theOperator = Not; break;
- default : theOperator = Illegal; break;
- }
- (*position)++;
- }
-
- ch = theSegment[*position];
-
- while(isalnum(ch) || (ch == '%') || (ch == '$'))
- /* read in item */
- {
- theResult[x] = ch;
- (*position)++;
- x++;
- ch = theSegment[*position];
- }
-
- theResult[x] = '\0'; /* terminate string */
-
- return(theOperator);
- }
-
- void nextLine(sHandle sourceCode)
- {
- char **startHandle;
- char ch;
- long caretPos,size;
-
-
- startHandle = (*sourceCode)->storage;
- caretPos = (*sourceCode)->caret;
- size = (*sourceCode)->size;
-
- while(((*startHandle)[caretPos] != '\r') && (caretPos <= size))
- caretPos++;
-
-
- if ((*startHandle)[caretPos] == '\0')
- AsmError = UnexpectedEndOfFile;
- else
- (*sourceCode)->line++;
-
- caretPos++;
-
- (*sourceCode)->storage = startHandle;
- (*sourceCode)->caret = caretPos;
- }
-
- /* private */
-
-
-
-
-
-
-
-
-
-